<?php
class ModelCatalogCarBranch extends Model {
	public function getCarBranch($car_branch_id) {

        $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "car_branch c WHERE  c.car_branch_id = '" . (int)$car_branch_id . "' AND c.status = '1'");
		
		return $query->row;
	}
	
	public function getCarBranches($parent_id = 0) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "car_branch c WHERE c.parent_id = '" . (int)$parent_id . "' AND c.status = '1' ORDER BY c.sort_order, LCASE(c.name)");
		
		return $query->rows;
	}

	public function getCarBranchesByParentId($car_branch_id) {
		$car_branch_data = array();
		
		$car_branch_data[] = $car_branch_id;
		
		$car_branch_query = $this->db->query("SELECT car_branch_id FROM " . DB_PREFIX . "car_branch WHERE parent_id = '" . (int)$car_branch_id . "'");
		
		foreach ($car_branch_query->rows as $car_branch) {
			$children = $this->getCarBranchesByParentId($car_branch['car_branch_id']);
			
			if ($children) {
				$car_branch_data = array_merge($children, $car_branch_data);
			}			
		}
		
		return $car_branch_data;
	}
		
	public function getCarBranchLayoutId($car_branch_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "car_branch_to_layout WHERE car_branch_id = '" . (int)$car_branch_id . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "'");
		
		if ($query->num_rows) {
			return $query->row['layout_id'];
		} else {
			return $this->config->get('config_layout_car_branch');
		}
	}
					
	public function getTotalCarBranchesByCarBranchId($parent_id = 0) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "car_branch c LEFT JOIN " . DB_PREFIX . "car_branch_to_store c2s ON (c.car_branch_id = c2s.car_branch_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1'");
		
		return $query->row['total'];
	}
}
?>